package com.roy.eureka.client.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.roy.eureka.api.dto.User;
import com.roy.eureka.client.service.HystrixWebClient;
import com.roy.eureka.client.service.UserService;

@RestController
@RequestMapping(value="/Feign")
public class FeignController {

	@Autowired
	private UserService userSerivce;
	@RequestMapping(value="/getAllUsers",method=RequestMethod.GET)
	@ResponseBody
	public Object getAllUsers(@RequestParam int i){
		return userSerivce.getUser(i);
	}
	
	@RequestMapping(value="/getSomeUsers",method=RequestMethod.GET)
	@ResponseBody
	public Object getSomeUsers(@RequestParam int countNum) {
		List<User> users = new ArrayList<User>();
		for(int i = 0 ; i < countNum ; i ++) {
			User user = new User();
			user.setUserId(""+i);
			users.add(user);
		}
//		String s_users=JSON.toJSONString(users);
		return userSerivce.getSomeUsers(users);
	}
	
	@HystrixCommand(fallbackMethod="findUserByFallBack",commandProperties= {
		@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000"),
		@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value="10000"),
	})
	@RequestMapping(value="/getOneUsers/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Object getOneUsers(@PathVariable("id")String userId) {
		User user = new User();
		user.setUserId(userId);
		return userSerivce.getOneUsers(user);
	}
	
	@RequestMapping(value="/getStringInfo",method=RequestMethod.GET)
	@ResponseBody
	public Object getStringInfo(String testStr) {
		return userSerivce.getStringInfo(testStr);
	}
	/**
	 * hystrix的默认回调方法必须与原方法结构一样。包括参数和返回类型 
	 */
	public Object findUserByFallBack(String userId) {
		User user = new User();
		user.setUserId("-1");
		user.setUserName("fallBackUser");
		return user;
	}
	/**
	 * hystrix的web客户端示例
	 */
	@Autowired
	private HystrixWebClient hystrixClient;
	@RequestMapping(method = RequestMethod.GET, value = "/HystrixDemo")
	public String HystrixDemo() {
		return hystrixClient.hello();
	}
	@RequestMapping(method = RequestMethod.GET, value = "/HystrixDemo2")
	public String HystrixDemo2() {
		String result = hystrixClient.toHello();
		HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
				.getInstance(HystrixCommandKey.Factory
						.asKey("HystrixWebClient#toHello()"));	
		System.out.println("断路器状态：" + breaker.isOpen());
		return result;
	}
}
